package cn.jjxx.modules.workflow.controller;

import cn.jjxx.core.model.AjaxJson;
import cn.jjxx.core.model.PageJson;
import cn.jjxx.core.query.annotation.PageableDefaults;
import cn.jjxx.core.query.data.PropertyPreFilterable;
import cn.jjxx.core.query.data.Queryable;
import cn.jjxx.core.query.utils.QueryableConvertUtils;
import cn.jjxx.core.query.wrapper.EntityWrapper;
import cn.jjxx.core.security.shiro.authz.annotation.RequiresMethodPermissions;
import cn.jjxx.core.utils.ObjectUtils;
import cn.jjxx.core.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import cn.jjxx.core.common.controller.BaseBeanController;
import cn.jjxx.core.security.shiro.authz.annotation.RequiresPathPermission;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import cn.jjxx.modules.activiti.service.IFlowService;
import cn.jjxx.modules.workflow.entity.ActSignTemplate;
import cn.jjxx.modules.workflow.service.IActSignTemplateService;

/**   
 * @Title: 流程签字模板
 * @Description: 流程签字模板
 * @author zcg
 * @date 2018-04-17 21:25:15
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("${admin.url.prefix}/workflow/actsigntemplate")
@RequiresPathPermission("workflow:actsigntemplate")
public class ActSignTemplateController extends BaseBeanController<ActSignTemplate> {

    @Autowired
    protected IActSignTemplateService actSignTemplateService;
    
    @Autowired
    protected IFlowService flowService;

	/**
	 * @Description: 跳转至流程签字模板的列表界面 .<br>
	 * @author zcg .<br>
	 * @date 2018-04-17 21:25:15.<br>
	 */
    @RequiresMethodPermissions("list")
    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model, HttpServletRequest request, HttpServletResponse response) {
        return display("list");
    }

	/**
	 * @Description: 流程签字模板的列表界面获取列表结果 .<br>
	 * @author zcg .<br>
	 * @date 2018-04-17 21:25:15.<br>
	 */
    @RequestMapping(value = "ajaxList", method = { RequestMethod.GET, RequestMethod.POST })
    @PageableDefaults(sort = "id=desc")
    private void ajaxList(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,
                          HttpServletResponse response) throws IOException {
        EntityWrapper<ActSignTemplate> entityWrapper = new EntityWrapper<ActSignTemplate>(entityClass);
        propertyPreFilterable.addQueryProperty("id");
        // 预处理
        QueryableConvertUtils.convertQueryValueToEntityValue(queryable, entityClass);
        SerializeFilter filter = propertyPreFilterable.constructFilter(entityClass);
        PageJson<ActSignTemplate> pagejson = new PageJson<ActSignTemplate>(actSignTemplateService.list(queryable,entityWrapper));
        String content = JSON.toJSONString(pagejson, filter);
        StringUtils.printJson(response, content);
    }

	/**
	 * @Description: 跳转至流程签字模板的添加界面 .<br>
	 * @author zcg .<br>
	 * @date 2018-04-17 21:25:15.<br>
	 */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String create(Model model, HttpServletRequest request, HttpServletResponse response) {
    	String preDefId = request.getParameter("preDefId");
    	AjaxJson ajaxJson = findSignTemplate(preDefId, null, null);
    	ActSignTemplate signTemplate = new ActSignTemplate();
    	signTemplate.setPreDefId(preDefId);
        if(!ObjectUtils.isNullOrEmpty(ajaxJson.getData())){
        	signTemplate = (ActSignTemplate) ajaxJson.getData();
        }
        model.addAttribute("data", signTemplate);
        return display("edit");
    }
    
	/**
	 * @Description: 执行保存流程签字模板的数据操作 .<br>
	 * @param actSignTemplate 流程签字模板实体类.<br>
	 * @author zcg .<br>
	 * @date 2018-04-17 21:25:15.<br>
	 */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson doSave(ActSignTemplate actSignTemplate, HttpServletRequest request, HttpServletResponse response) {
        AjaxJson ajaxJson = new AjaxJson();
        ajaxJson.success("保存成功");
        boolean flag = actSignTemplateService.save(actSignTemplate);
        if(!flag){
        	ajaxJson.success("保存失败");
        	ajaxJson.setRet(AjaxJson.RET_FAIL);
        }
        return ajaxJson;
    }
    
    /**
     * @Description: 通过条件，获取签字模板 .<br>
     * @param preDefId 流程定义Id.<br>
     * @param menuId 菜单Id(可以为null).<br>
     * @param orgId 组织Id（可以为null）.<br>
     * @author 郑成功 .<br>
     * @date 2018-4-17 下午9:41:03.<br>
     */
    @RequestMapping(value = "/findSignTemplate")
    @ResponseBody
    public AjaxJson findSignTemplate(String preDefId,String menuId,String orgId){
    	AjaxJson ajaxJson = new AjaxJson();
    	EntityWrapper<ActSignTemplate> wrapper = new EntityWrapper<ActSignTemplate>(entityClass);
    	wrapper.eq("preDefId", preDefId);
    	if(StringUtils.isNotEmpty(orgId)){
    		wrapper.eq("orgId", orgId);
    	}
    	if(StringUtils.isNotEmpty(menuId)){
    		wrapper.eq("menuId", menuId);
    	}
    	List<ActSignTemplate> list = actSignTemplateService.selectList(wrapper);
    	//ajaxJson.setData(new ActSignTemplate());
    	if(list.size()>0){
    		ajaxJson.setData(list.get(0));
    	}
    	return ajaxJson;
    }
    
    /**
     * @Description: 根据单据ID，获取签字模板 .<br>
     * @author huangqiling.<br>
     * @date 2018-8-22 下午9:41:03.<br>
     */
    @RequestMapping(value = "getSignRecordList", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
    public AjaxJson getSignRecordList(HttpServletRequest request,HttpServletResponse response){
    	AjaxJson ajaxJson = new AjaxJson();
    	String billId = request.getParameter("billId");
    	String sign = actSignTemplateService.replaceSignContents(billId, flowService);
    	sign = sign!=null?sign:"";
    	ajaxJson.setMsg(sign);
    	return ajaxJson;
    }
    
}
